//
//  WBPictureView.swift
//  SinaWeibo
//
//  Created by 风不会停息 on 2016/11/18.
//  Copyright © 2016年 风不会停息. All rights reserved.
//

import UIKit
private let cellId = "HMPictureCellId"
class WBPictureView: UICollectionView {

    var pictureInfos: [WBPictureInfo]? {
        didSet {
            //外界给配图视图设置数据的时候就会执行
            self.reloadData()
        }
    }
    
    override func awakeFromNib() {
        //注册cell
        let pictureCellNib = UINib(nibName: "WBPictureCell", bundle: nil)
        self.register(pictureCellNib, forCellWithReuseIdentifier: cellId)
        
        //显示数据 遵守dataSource协议 实现数据方法
        self.dataSource = self
    }

}

extension WBPictureView: UICollectionViewDataSource {
    //实现数据源方法
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! WBPictureCell
        let pictureInfo = self.pictureInfos![indexPath.item]
        cell.pictureInfo = pictureInfo
        return cell
    }
}
